package soma.rest.training.jerseySpringJDO.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.jdo.JdoTemplate;
import org.springframework.orm.jdo.support.JdoDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import soma.rest.training.jerseySpringJDO.model.Message;

import com.google.appengine.api.datastore.Key;
import com.google.storage.onestore.v3.OnestoreEntity.Property.Meaning;


@Repository("messageJdoTemplateDAO")
public class MessageJdoTemplateDAOImpl {
	@Autowired
	private PersistenceManagerFactory pmf;
	
	@Autowired
	public JdoTemplate jdoTemplate;

	@Transactional(readOnly=true)
	public Message get(Long id){		

		Message message = jdoTemplate.getObjectById(Message.class, id);

		if(message==null){
			throw new ObjectRetrievalFailureException(Message.class, id);
		}

		return jdoTemplate.detachCopy(message);
	}

	@Transactional
	public Message save(Message message){	
		Message returnMessage = jdoTemplate.makePersistent(message);
		return jdoTemplate.detachCopy(returnMessage);
	}
	
	@Transactional
	public void delete(Long id){	
		Message message = get(id);
		jdoTemplate.deletePersistent(message);
	}
	
	@Transactional(readOnly=true)
	public Collection<Message> search(String word){
		
		Collection<Message> msgs = jdoTemplate.find(Message.class, "text == input", "String input", word);
		return jdoTemplate.detachCopyAll(msgs);
		
		
	}	
}
